//
//  KDThirdPostCell.m
//  WangJingApp
//
//  Created by 魏强 on 17/2/8.
//  Copyright © 2017年 KDTech. All rights reserved.
//

#import "KDThirdPostCell.h"
#import "KDUnitPerson.h"
@interface KDThirdPostCell ()
@property (weak, nonatomic) IBOutlet UIButton *selectBtn;
@property (weak, nonatomic) IBOutlet UILabel *unitNameLb;
/**  右边那个显示具体选中多少人的label */
@property (weak, nonatomic) IBOutlet UILabel *rightLb;

@end
@implementation KDThirdPostCell
#pragma mark - 生命周期区
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    NSString *cellId =@"KDThirdPostCell";
    KDThirdPostCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell =[[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil]lastObject];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    //    cell.contentView.backgroundColor = BackGroundColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)setModel:(KDUnit *)model{
    _model = model;
    self.selectBtn.selected = model.select;
//    KDLog(@"%d", model.select);
    self.unitNameLb.text = [NSString stringWithFormat:@"%@(%ld)",model.unit_name,model.members.count];
    int selectNum = 0;
    for (KDUnitPerson *member in model.members) {
        if (member.select) {//如果已经选中了，就＋1
            selectNum ++;
        }
    }
    //如果是全部或者全无，就不显示
    if ( selectNum == 0) {
        self.rightLb.text = nil;
    }else{
        self.rightLb.text = [NSString stringWithFormat:@"%d/%ld",selectNum,model.members.count];
    }
}
- (IBAction)selectBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.model.allSelect = sender.selected;
    self.rightLb.text = (sender.selected)?[NSString stringWithFormat:@"%lu/%ld",(unsigned long)_model.members.count,_model.members.count]:nil;
    if (_delegate && [_delegate respondsToSelector:@selector(thirdPostCell:selectBtnClick:)]) {
        [_delegate thirdPostCell:self selectBtnClick:sender];
    }
    
}

- (IBAction)rightBtnClick:(UIButton *)sender{
    if (_delegate && [_delegate respondsToSelector:@selector(thirdPostCell:rightBtnClick:)]) {
        [_delegate thirdPostCell:self rightBtnClick:sender];
    }
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
//    [self selectBtnClick:self.selectBtn];
    // Configure the view for the selected state
}

@end
